import React, { Component } from "react";
import { SafeAreaView, Text, View, StyleSheet, TouchableOpacity, Image, TextInput, PermissionsAndroid, Platform, DeviceEventEmitter } from 'react-native';
import Constant from '../../utils/Constant'//全局变量
import HttpUtils from "../../utils/HttpUtils";
import HttpUrls from "../../utils/HttpUrls";
import Header from "../../components/header";
import { ToastToUser } from "../../utils/CustomToast";
const isIos = Platform.OS == 'ios';

/**
 * 修改订单价格
 */
export default class ModifyPricePage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            price: this.props.route.params.price ? this.props.route.params.price : '',
            priceValue: '',
            orderNo: this.props.route.params.orderNo
        };
    }

    componentDidMount() {
        if (this.state.price) {
            this.setState({
                // priceValue: this.state.price / (100 * 10000) + ''
                priceValue: (this.state.price / 100).toFixed(0) + ''
            })
        }
    }

    render() {
        const navigation = this.props.navigation;
        return (
            <View
                style={styles.container}>
                <Header navigation={navigation} Title={'修改价格'} hideSepLine={true} />

                <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 10, width: '100%', justifyContent: 'flex-start', paddingLeft: 10, paddingRight: 10, backgroundColor: '#fff' }}>
                    <Text style={{ color: '#333333', fontSize: 14 }}>成交价：</Text>
                    <View style={{ flex: 1 }}>
                        <TextInput
                            style={styles.inputStyle}
                            value={this.state.priceValue}
                            placeholder="请输入"
                            onChangeText={(text) => this.setState({ priceValue: text })}
                            inputMode="decimal">

                        </TextInput>
                    </View>
                    <Text style={{ color: '#333333', fontSize: 14 }}>元</Text>
                </View>





                <TouchableOpacity
                    style={styles.btnConfirm}
                    onPress={() => {
                        this.priceSubmit()
                    }}>
                    <Text style={{ color: '#333333', fontSize: 14 }}>确认修改</Text>
                </TouchableOpacity>

            </View>
        );
    }

    priceSubmit() {

        if (this.state.priceValue.length == 0) {
            ToastToUser('请输入成交价')
            return
        }

        const scaledValue = (parseFloat(this.state.priceValue) * 100).toFixed(0); 
        
        const finalPrice = ((scaledValue * 100) / 100).toFixed(0);

        console.log('finalPrice', finalPrice)


        if (finalPrice > this.state.price) {
            ToastToUser('请设置低于当前的价格')
            return
        }

        let params = {
            orderNo: this.state.orderNo,
            quotationAmount: finalPrice
        }

        HttpUtils.getRequest(HttpUrls.changeOrderAmount, params)
            .then((result) => {
                let code = result.code
                if (code == 0) {
                    ToastToUser('修改成功')
                    if (this.props.route.params.onConfirm) {
                        this.props.route.params.onConfirm(this.state.priceValue)
                    }
                    this.props.navigation.goBack()
                } else {
                    ToastToUser(result.msg + '')
                }
            })
            .catch((error) => {

            });
    }

};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#f9f9f9',
    },


    inputStyle: {

        flex1: 1,
        backgroundColor: '#fff',
        padding: 10,
        fontSize: 14,
        color: '#333333'
    },

    btnConfirm: {
        height: 44,

        // backgroundColor: '#fff',
        justifyContent: 'center',
        alignItems: 'center',
        marginTop: 30,
        backgroundColor: '#FFE814',
        marginLeft: 10,
        marginRight: 10,
        borderRadius: 6
    }

});